
const regeneratorRuntime = require("../../vendor/regenerator-runtime")
const co = require("../../vendor/co")
Page({
    onLoad: function (options) {
        this.setData(options)
        function f() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log("a")
                    resolve()
                }, 1000)
            })
        }

        co(function* () {
            for (var i = 0; i < 3; i++) {
                const r = yield f()
            }

            console.log('---')
        })




    },
    onReady() {
        this.setData({
            imgSize: parseInt((getApp().window.width - 20) / 2)
        })
    },

    
    loadCard: function () {
        wx.showLoading({
            title: '加载中...',
        })
        const data = {}
        this.data.id && (data.id = this.data.id)
        getApp().http.get("city/member-card/card-detail", { data, showMsg: false }).then(res => {
            wx.hideLoading()
            this.setData({
                mid: wx.getStorageSync("mid"),
                hasCard: res.data.is_has_card
            })
            if (this.data.mid != res.data.id) {
                //查看名片
                if (!res.data.info) {
                    wx.showModal({
                        title: '提示',
                        content: 'Ta还没有名片噢~',
                        showCancel: false,
                        confirmText: "我知道了",
                        success: res => {
                            wx.navigateBack({

                            })
                        }
                    })
                    return
                }

            }
            if (!res.data.info.cityMemberCard) {
                wx.showModal({
                    title: '提示',
                    content: '您还没有名片哦',
                    confirmText: "去创建+",
                    cancelText: "我知道了",
                    success: res => {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: './create-card',
                            })
                        } else {
                            wx.navigateBack({

                            })
                        }
                    }
                })
            } else {
                const card = res.data.info.cityMemberCard
                wx.setNavigationBarTitle({
                    title: res.data.info.nickname ? (res.data.info.nickname + "的名片") : "名片"
                })
                delete res.data.info.cityMemberCard
                this.setData({
                    isKeep: res.data.is_keep,
                    card,
                    info: res.data.info
                })
            }
        }).catch(err => {
            wx.showModal({
                title: '提示',
                content: err.message,
                showCancel: false,
                confirmText: "我知道了",
                success: res => {
                    wx.navigateBack({

                    })
                }
            })
        })
    },
    onShow() {
        this.loadCard();
    },
    /**
     * 我的名片夹
     */
    cardList: function () {
        wx.redirectTo({
            url: 'card-list'
        })
    },
    /**
     * 编辑名片
     */
    updateCard: function () {
        wx.navigateTo({
            url: 'create-card?id=' + this.data.info.id
        })
    },
    /**
    * 个人名片小程序码
    */
    openQrcode: function () {
        var imgUrl = getApp().http.getBaseUrl() + "city/member-card/card-qrcode" + '?id=' + this.data.info.id;
        console.log(imgUrl)
        wx.previewImage({
            urls: [imgUrl],
        });
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let t = '您好，这是我的名片,请惠存'
        if (this.data.info.id != this.data.mid) {
            t = `您好，这是${this.data.card.name || this.data.info.nickname}的名片,请惠存`
        }
        return {
            title: t,
            path: '/pages/card/my-card?id=' + this.data.info.id,
        }
    },

    preImg(evt) {
        wx.previewImage({
            current: evt.currentTarget.dataset.img,
            urls: this.data.card.cityMemberCardImages.map(o => o.url),
        })
    },

    keep() {
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.get("city/member-card/keep", {
            data: { id: this.data.card.id }
        }).then(res => {
            this.loadCard()
        })
    },

    save() {
        wx.addPhoneContact({
            firstName: this.data.card.name || this.data.info.nickname,
            mobilePhoneNumber: this.data.card.phone,
            organization: this.data.card.company,
            title: this.data.card.job,
            email: this.data.card.email
        })
    },
    viewQr() {

        wx.previewImage({
            urls: [getApp().http.getBaseUrl() + "city/member-card/new-qr?id=" + this.data.card.id + "&sid=" + getApp().storeId],
        })

        return
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.get("city/member-card/qr", {
            data: {
                id: this.data.card.id
            }
        }).then(res => {
            wx.previewImage({
                urls: [res.data],
            })
            wx.hideLoading()
        })
    },

    like() {

        getApp().http.get("city/member-card/like", {
            data: {
                id: this.data.card.id
            }
        }).then(res => {
            let num = parseInt(this.data.card.like_num)
            if (res.data === true) {
                num--
                this.setData({
                    signNum: -1
                })
            } else {
                num++
                this.setData({
                    signNum: "+1"
                })
            }
            this.setData({
                "card.like_num": num
            })

            this.setData({
                isLike: true
            })
            clearTimeout(this.lid)
            this.lid = setTimeout(() => {
                this.setData({
                    isLike: false
                })
            }, 1000)
        })
    },
    copy(evt) {
        const { t, val } = evt.currentTarget.dataset
        wx.showActionSheet({
            itemList: ['复制'],
            success: res => {
                wx.setClipboardData({
                    data: val,
                })
                wx.showToast({
                    title: t + "已复制",
                })
            }
        })
    },
    call(evt) {
        this.data.card.phone && wx.makePhoneCall({
            phoneNumber: this.data.card.phone,
        })
    },
    back() {
        const ps = getCurrentPages()
        if (ps.length == 1) {
            wx.redirectTo({
                url: '/pages/card/my-card',
            })
        } else {
            wx.navigateBack()
        }
    },
    openMap() {
        wx.openLocation({
            latitude: parseFloat(this.data.card.lat),
            longitude: parseFloat(this.data.card.lng),
            address: this.data.card.address
        })
    },
    action(evt) {
        const { path } = evt.currentTarget.dataset
        if (this.data.id && this.data.id != this.data.mid) {
            //查看别人的名片时
            if (!this.data.hasCard) {
                wx.showModal({
                    title: '提示',
                    content: '您需要先创建自己的名片哦',
                    cancelText: "我知道了",
                    confirmText: "去创建+",
                    success: res => {
                        res.confirm && wx.navigateTo({
                            url: 'create-card',
                        })
                    }
                })
            } else {
                wx.navigateTo({
                    url: path,
                })
            }
        } else {
            wx.navigateTo({
                url: path,
            })
        }
    },
    like() {
        getApp().http.get("city/member-card/like", {
            data: { id: this.data.card.id }
        }).then(res => {
            let num = parseInt(this.data.card.like_num)
            num += (res.data.status ? 1 : -1)
            this.setData({
                'card.like_num': num
            })
        })
    },
})